From 9aeb0a64c865b04cce67212b9c8b1ed0f8d767d8 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 30 Jan 2022 07:46:32 -0700 Subject: [PATCH] convert humminbird to Format class. (#840) * convert humminbird to Format class. * const member functions, constexpr * sorting members, easing off on forced init of auto variables. * iwyu --- CMakeLists.txt | 1 + GPSBabel.pro | 1 + humminbird.cc | 267 ++++++++++++++++++------------------------------- humminbird.h | 223 +++++++++++++++++++++++++++++++++++++++++ vecs.h | 7 +- 5 files changed, 325 insertions(+), 174 deletions(-) create mode 100644 humminbird.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 38ffddd79..94a9a38c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,6 +244,7 @@ set(HEADERS gtrnctr.h heightgrid.h holux.h + humminbird.h html.h inifile.h kml.h diff --git a/GPSBabel.pro b/GPSBabel.pro index bf0c2c78d..d755cd205 100644 --- a/GPSBabel.pro +++ b/GPSBabel.pro @@ -231,6 +231,7 @@ HEADERS = \ gtrnctr.h \ heightgrid.h \ holux.h \ + humminbird.h \ html.h \ inifile.h \ kml.h \ diff --git a/humminbird.cc b/humminbird.cc index ac628b9a3..baadd27e8 100644 --- a/humminbird.cc +++ b/humminbird.cc @@ -19,10 +19,18 @@ */ -#include "defs.h" -#include -#include -#include +#include "humminbird.h" + +#include // for QMap +#include // for CaseInsensitive + +#include // for atan, tan, M_PI, log, sinh +#include // for snprintf, SEEK_SET +#include // for strncpy, memcpy, memset + +#include "defs.h" // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, si_round, xcalloc, track_add_wpt, xstrndup, mkshort, mkshort_del_handle, mkshort_new_handle, setshort_badchars, setshort_defname, setshort_length, setshort_mustuniq, setshort_... +#include "src/core/datetime.h" // for DateTime + #define MYNAME "humminbird" @@ -59,7 +67,7 @@ Still, they're useful in the code as a plain signature. /* The hwr data format is records-based, and the records are 36 bytes long. */ -struct humminbird_waypt_t { +struct HumminbirdBase::humminbird_waypt_t { /* O.K.: the file can also contain routes with a different magic. */ /* uint32_t signature; */ /* Just for error checking(?) */ uint16_t num; /* Always ascending in the file. */ @@ -74,7 +82,7 @@ struct humminbird_waypt_t { char name[WPT_NAME_LEN]; }; -struct humminbird_rte_t { +struct HumminbirdBase::humminbird_rte_t { /* O.K.: the file can contain also routes with a different magic. */ /* uint32_t signature; */ /* Just for error checking(?) */ uint16_t num; @@ -88,7 +96,7 @@ struct humminbird_rte_t { uint16_t points[MAX_RTE_POINTS]; }; -struct humminbird_trk_header_t { /* 68 bytes, incl signature */ +struct HumminbirdBase::humminbird_trk_header_t { /* 68 bytes, incl signature */ /* uint32_t signature; */ uint16_t trk_num; uint16_t zero; @@ -110,13 +118,13 @@ struct humminbird_trk_header_t { /* 68 bytes, incl signature */ }; -struct humminbird_trk_point_t { +struct HumminbirdBase::humminbird_trk_point_t { int16_t deltaeast; int16_t deltanorth; uint16_t depth; /* in centimeters */ }; -struct humminbird_trk_header_old_t { /* 16 bytes, incl signature */ +struct HumminbirdBase::humminbird_trk_header_old_t { /* 16 bytes, incl signature */ /* uint32_t signature; */ uint16_t trk_num; uint16_t zero; @@ -131,12 +139,12 @@ struct humminbird_trk_header_old_t { /* 16 bytes, incl signature */ }; -struct humminbird_trk_point_old_t { +struct HumminbirdBase::humminbird_trk_point_old_t { int16_t deltaeast; int16_t deltanorth; }; -struct group_header_t { +struct HumminbirdBase::group_header_t { uint8_t status; uint8_t icon; uint16_t depth; @@ -148,7 +156,7 @@ struct group_header_t { char name[WPT_NAME_LEN]; }; -struct group_body_t { +struct HumminbirdBase::group_body_t { uint8_t status; uint8_t icon; uint16_t next_idx; @@ -156,58 +164,13 @@ struct group_body_t { }; -static const char* humminbird_icons[] = { - "Normal", /* 0 */ - "House", /* 1 */ - "Red cross", /* 2 */ - "Fish", /* 3 */ - "Duck", /* 4 */ - "Anchor", /* 5 */ - "Buoy", /* 6 */ - "Airport", /* 7 */ - "Camping", /* 8 */ - "Danger", /* 9 */ - "Fuel", /* 10 */ - "Rock", /* 11 */ - "Weed", /* 12 */ - "Wreck", /* 13 */ - "Phone", /* 14 */ - "Coffee", /* 15 */ - "Beer", /* 16 */ - "Mooring", /* 17 */ - "Pier", /* 18 */ - "Slip", /* 19 */ - "Ramp", /* 20 */ - "Circle", /* 21 */ - "Diamond", /* 22 */ - "Flag", /* 23 */ - "Pattern", /* 24 */ - "Shower", /* 25 */ - "Water tap", /* 26 */ - "Tree", /* 27 */ - "Recording", /* 28 */ - "Snapshot" /* 29 */ -}; - -static gbfile* fin_; -static gbfile* fout_; -static int waypoint_num; -static short_handle wptname_sh, rtename_sh, trkname_sh; -static humminbird_rte_t* humrte; -static int rte_num_; -static QMap map; - -static -QVector humminbird_args = { -}; - /* Takes a latitude in degrees, * returns a latitude in degrees. */ -static double -geodetic_to_geocentric_hwr(const double gd_lat) +double +HumminbirdBase::geodetic_to_geocentric_hwr(const double gd_lat) { - const double cos_ae = 0.9966349016452; - const double cos2_ae = cos_ae * cos_ae; + constexpr double cos_ae = 0.9966349016452; + constexpr double cos2_ae = cos_ae * cos_ae; const double gdr = gd_lat *M_PI / 180.0; return atan(cos2_ae * tan(gdr)) * 180.0/M_PI; @@ -215,19 +178,19 @@ geodetic_to_geocentric_hwr(const double gd_lat) /* Takes a latitude in degrees, * returns a latitude in degrees. */ -static double -geocentric_to_geodetic_hwr(const double gc_lat) +double +HumminbirdBase::geocentric_to_geodetic_hwr(const double gc_lat) { - const double cos_ae = 0.9966349016452; - const double cos2_ae = cos_ae * cos_ae; + constexpr double cos_ae = 0.9966349016452; + constexpr double cos2_ae = cos_ae * cos_ae; const double gcr = gc_lat *M_PI / 180.0; return atan(tan(gcr)/cos2_ae) * 180.0/M_PI; } /* Takes a projected "north" value, returns latitude in degrees. */ -static double -gudermannian_i1924(const double x) +double +HumminbirdBase::gudermannian_i1924(const double x) { const double norm_x = x/i1924_equ_axis; @@ -235,8 +198,8 @@ gudermannian_i1924(const double x) } /* Takes latitude in degrees, returns projected "north" value. */ -static double -inverse_gudermannian_i1924(const double x) +double +HumminbirdBase::inverse_gudermannian_i1924(const double x) { const double x_r = x/180.0 * M_PI; const double guder = log(tan(M_PI/4.0 + x_r/2.0)); @@ -248,20 +211,20 @@ inverse_gudermannian_i1924(const double x) * %%% global callbacks called by gpsbabel main process %%% * *******************************************************************************/ -static void -humminbird_rd_init(const QString& fname) +void +HumminbirdBase::humminbird_rd_init(const QString& fname) { fin_ = gbfopen_be(fname, "rb", MYNAME); } -static void -humminbird_rd_deinit() +void +HumminbirdBase::humminbird_rd_deinit() const { gbfclose(fin_); } -static void -humminbird_read_wpt(gbfile* fin) +void +HumminbirdBase::humminbird_read_wpt(gbfile* fin) { humminbird_waypt_t w; @@ -327,8 +290,8 @@ humminbird_read_wpt(gbfile* fin) map[buff] = wpt; } -static void -humminbird_read_route(gbfile* fin) +void +HumminbirdBase::humminbird_read_route(gbfile* fin) const { humminbird_rte_t hrte; @@ -366,8 +329,8 @@ humminbird_read_route(gbfile* fin) } } -static void -humminbird_read_track(gbfile* fin) +void +HumminbirdBase::humminbird_read_track(gbfile* fin) { humminbird_trk_header_t th; @@ -477,12 +440,12 @@ humminbird_read_track(gbfile* fin) xfree(points); } -static void -humminbird_read_track_old(gbfile* fin) +void +HumminbirdBase::humminbird_read_track_old(gbfile* fin) { humminbird_trk_header_old_t th; - const int file_len = 8048; + constexpr int file_len = 8048; char namebuf[TRK_NAME_LEN]; @@ -584,8 +547,8 @@ humminbird_read_track_old(gbfile* fin) xfree(points); } -static void -humminbird_read() +void +HumminbirdBase::humminbird_read() { while (! gbfeof(fin_)) { uint32_t signature = gbfgetuint32(fin_); @@ -613,8 +576,8 @@ humminbird_read() /************************************************************************************************/ -static void -humminbird_wr_init(const QString& fname) +void +HumminbirdBase::humminbird_wr_init(const QString& fname) { fout_ = gbfopen_be(fname, "wb", MYNAME); @@ -650,8 +613,8 @@ humminbird_wr_init(const QString& fname) rte_num_ = 0; } -static void -humminbird_wr_deinit() +void +HumminbirdBase::humminbird_wr_deinit() { mkshort_del_handle(&wptname_sh); mkshort_del_handle(&rtename_sh); @@ -659,8 +622,8 @@ humminbird_wr_deinit() gbfclose(fout_); } -static void -humminbird_write_waypoint(const Waypoint* wpt) +void +HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt) { humminbird_waypt_t hum; int num_icons = sizeof(humminbird_icons) / sizeof(humminbird_icons[0]); @@ -715,15 +678,8 @@ humminbird_write_waypoint(const Waypoint* wpt) gbfwrite(&hum, sizeof(hum), 1, fout_); } -static humminbird_trk_header_t* trk_head; -static humminbird_trk_point_t* trk_points; -static int32_t last_east; -static int32_t last_north; -static uint32_t last_time; - - -static void -humminbird_track_head(const route_head* trk) +void +HumminbirdHTFormat::humminbird_track_head(const route_head* trk) { int max_points = (131080 - sizeof(uint32_t)- sizeof(humminbird_trk_header_t)) / sizeof(humminbird_trk_point_t); @@ -739,8 +695,8 @@ humminbird_track_head(const route_head* trk) } } -static void -humminbird_track_tail(const route_head*) +void +HumminbirdHTFormat::humminbird_track_tail(const route_head* /*unused*/) { int max_points = (131080 - sizeof(uint32_t)- sizeof(humminbird_trk_header_t)) / sizeof(humminbird_trk_point_t); @@ -776,8 +732,8 @@ humminbird_track_tail(const route_head*) trk_points = nullptr; } -static void -humminbird_track_cb(const Waypoint* wpt) +void +HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt) { if (trk_head == nullptr) { return; @@ -841,15 +797,23 @@ humminbird_track_cb(const Waypoint* wpt) } -static void -humminbird_track_write() +void +HumminbirdHTFormat::write() { - - track_disp_all(humminbird_track_head, humminbird_track_tail, humminbird_track_cb); + auto humminbird_track_head_lambda = [this](const route_head* rte)->void { + humminbird_track_head(rte); + }; + auto humminbird_track_tail_lambda = [this](const route_head* rte)->void { + humminbird_track_tail(rte); + }; + auto humminbird_track_cb_lambda = [this](const Waypoint* waypointp)->void { + humminbird_track_cb(waypointp); + }; + track_disp_all(humminbird_track_head_lambda, humminbird_track_tail_lambda, humminbird_track_cb_lambda); } -static void -humminbird_rte_head(const route_head* rte) +void +HumminbirdFormat::humminbird_rte_head(const route_head* rte) { humrte = nullptr; if (rte->rte_waypt_ct() > 0) { @@ -857,8 +821,8 @@ humminbird_rte_head(const route_head* rte) } } -static void -humminbird_rte_tail(const route_head* rte) +void +HumminbirdFormat::humminbird_rte_tail(const route_head* rte) { if (humrte == nullptr) { return; @@ -885,8 +849,8 @@ humminbird_rte_tail(const route_head* rte) humrte = nullptr; } -static void -humminbird_write_rtept(const Waypoint* wpt) +void +HumminbirdFormat::humminbird_write_rtept(const Waypoint* wpt) const { if (humrte == nullptr) { return; @@ -905,8 +869,8 @@ humminbird_write_rtept(const Waypoint* wpt) } } -static void -humminbird_write_waypoint_wrapper(const Waypoint* wpt) +void +HumminbirdFormat::humminbird_write_waypoint_wrapper(const Waypoint* wpt) { char* key; Waypoint* tmpwpt; @@ -926,60 +890,23 @@ humminbird_write_waypoint_wrapper(const Waypoint* wpt) xfree(key); } -static void -humminbird_write() +void +HumminbirdFormat::write() { - waypt_disp_all(humminbird_write_waypoint_wrapper); - route_disp_all(nullptr, nullptr, humminbird_write_waypoint_wrapper); - route_disp_all(humminbird_rte_head, humminbird_rte_tail, humminbird_write_rtept); + auto humminbird_write_waypoint_wrapper_lambda = [this](const Waypoint* waypointp)->void { + humminbird_write_waypoint_wrapper(waypointp); + }; + waypt_disp_all(humminbird_write_waypoint_wrapper_lambda); + route_disp_all(nullptr, nullptr, humminbird_write_waypoint_wrapper_lambda); + + auto humminbird_rte_head_lambda = [this](const route_head* rte)->void { + humminbird_rte_head(rte); + }; + auto humminbird_rte_tail_lambda = [this](const route_head* rte)->void { + humminbird_rte_tail(rte); + }; + auto humminbird_write_rtept_lambda = [this](const Waypoint* waypointp)->void { + humminbird_write_rtept(waypointp); + }; + route_disp_all(humminbird_rte_head_lambda, humminbird_rte_tail_lambda, humminbird_write_rtept_lambda); } - -/**************************************************************************/ - -ff_vecs_t humminbird_vecs = { - ff_type_file, - { - (ff_cap)(ff_cap_read | ff_cap_write) /* waypoints */, - ff_cap_read /* tracks */, - (ff_cap)(ff_cap_read | ff_cap_write) /* routes */ - }, - humminbird_rd_init, - humminbird_wr_init, - humminbird_rd_deinit, - humminbird_wr_deinit, - humminbird_read, - humminbird_write, - nullptr, // humminbird_exit, - &humminbird_args, - CET_CHARSET_ASCII, 1 /* ascii is the expected character set */ - /* currently fixed !!! */ - , NULL_POS_OPS, - nullptr -}; - -/**************************************************************************/ - -/**************************************************************************/ - -ff_vecs_t humminbird_ht_vecs = { - ff_type_file, - { - ff_cap_read /* waypoints */, - (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */, - ff_cap_read /* routes */ - }, - humminbird_rd_init, - humminbird_wr_init, - humminbird_rd_deinit, - humminbird_wr_deinit, - humminbird_read, - humminbird_track_write, - nullptr, // humminbird_exit, - &humminbird_args, - CET_CHARSET_ASCII, 1 /* ascii is the expected character set */ - /* currently fixed !!! */ - , NULL_POS_OPS, - nullptr -}; - -/**************************************************************************/ diff --git a/humminbird.h b/humminbird.h new file mode 100644 index 000000000..00b59da7f --- /dev/null +++ b/humminbird.h @@ -0,0 +1,223 @@ +/* + Copyright (C) 2008 Björn Augustsson, oggust@gmail.com + Copyright (C) 2008 Olaf Klein, o.b.klein@gpsbabel.org + Copyright (C) 2005-2013 Robert Lipe, robertlipe+source@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + */ +#ifndef HUMMINBIRD_H_INCLUDED_ +#define HUMMINBIRD_H_INCLUDED_ + +#include // for QMap +#include // for QString +#include // for QVector + +#include // for int32_t, uint32_t + +#include "defs.h" // for ff_cap, arglist_t, ff_cap_read, Waypoint, route_head, ff_cap_write, short_handle, CET_CHARSET_ASCII, ff_type, ff_type_file +#include "format.h" // for Format +#include "gbfile.h" // for gbfile + + +class HumminbirdBase +{ +protected: + /* Types */ + + struct humminbird_waypt_t; + struct humminbird_rte_t; + struct humminbird_trk_header_t; + struct humminbird_trk_point_t; + struct humminbird_trk_header_old_t; + struct humminbird_trk_point_old_t; + struct group_header_t; + struct group_body_t; + + /* Constants */ + + static constexpr const char* humminbird_icons[] = { + "Normal", /* 0 */ + "House", /* 1 */ + "Red cross", /* 2 */ + "Fish", /* 3 */ + "Duck", /* 4 */ + "Anchor", /* 5 */ + "Buoy", /* 6 */ + "Airport", /* 7 */ + "Camping", /* 8 */ + "Danger", /* 9 */ + "Fuel", /* 10 */ + "Rock", /* 11 */ + "Weed", /* 12 */ + "Wreck", /* 13 */ + "Phone", /* 14 */ + "Coffee", /* 15 */ + "Beer", /* 16 */ + "Mooring", /* 17 */ + "Pier", /* 18 */ + "Slip", /* 19 */ + "Ramp", /* 20 */ + "Circle", /* 21 */ + "Diamond", /* 22 */ + "Flag", /* 23 */ + "Pattern", /* 24 */ + "Shower", /* 25 */ + "Water tap", /* 26 */ + "Tree", /* 27 */ + "Recording", /* 28 */ + "Snapshot" /* 29 */ + }; + + /* Member Functions */ + + static double geodetic_to_geocentric_hwr(double gd_lat); + static double geocentric_to_geodetic_hwr(double gc_lat); + static double gudermannian_i1924(double x); + static double inverse_gudermannian_i1924(double x); + void humminbird_rd_init(const QString& fname); + void humminbird_rd_deinit() const; + void humminbird_read_wpt(gbfile* fin); + void humminbird_read_route(gbfile* fin) const; + static void humminbird_read_track(gbfile* fin); + static void humminbird_read_track_old(gbfile* fin); + void humminbird_read(); + void humminbird_wr_init(const QString& fname); + void humminbird_wr_deinit(); + + /* Data Members */ + + gbfile* fin_{}; + gbfile* fout_{}; + int waypoint_num{}; + short_handle wptname_sh{}, rtename_sh{}, trkname_sh{}; + humminbird_rte_t* humrte{}; + int rte_num_{}; + QMap map; + + humminbird_trk_header_t* trk_head{}; + humminbird_trk_point_t* trk_points{}; + int32_t last_east{}; + int32_t last_north{}; + uint32_t last_time{}; +}; + +class HumminbirdFormat : public Format, private HumminbirdBase +{ +public: + QVector* get_args() override + { + return &humminbird_args; + } + + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + return { + (ff_cap)(ff_cap_read | ff_cap_write) /* waypoints */, + ff_cap_read /* tracks */, + (ff_cap)(ff_cap_read | ff_cap_write) /* routes */ + }; + } + + QString get_encode() const override + { + return CET_CHARSET_ASCII; + } + + int get_fixed_encode() const override + { + return 1; + } + + void rd_init(const QString& fname) override {humminbird_rd_init(fname);} + void read() override {humminbird_read();} + void rd_deinit() override {humminbird_rd_deinit();} + void wr_init(const QString& fname) override {humminbird_wr_init(fname);} + void write() override; + void wr_deinit() override {humminbird_wr_deinit();} + +private: + /* Member Functions */ + + void humminbird_rte_head(const route_head* rte); + void humminbird_rte_tail(const route_head* rte); + void humminbird_write_rtept(const Waypoint* wpt) const; + void humminbird_write_waypoint(const Waypoint* wpt); + void humminbird_write_waypoint_wrapper(const Waypoint* wpt); + + /* Data Members */ + + QVector humminbird_args = { + }; +}; + +class HumminbirdHTFormat : public Format, private HumminbirdBase +{ +public: + QVector* get_args() override + { + return &humminbirdht_args; + } + + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + return { + ff_cap_read /* waypoints */, + (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */, + ff_cap_read /* routes */ + }; + } + + QString get_encode() const override + { + return CET_CHARSET_ASCII; + } + + int get_fixed_encode() const override + { + return 1; + } + + void rd_init(const QString& fname) override {humminbird_rd_init(fname);} + void read() override {humminbird_read();} + void rd_deinit() override {humminbird_rd_deinit();} + void wr_init(const QString& fname) override {humminbird_wr_init(fname);} + void write() override; + void wr_deinit() override {humminbird_wr_deinit();} + +private: + /* Member Functions */ + + void humminbird_track_head(const route_head* trk); + void humminbird_track_tail(const route_head* unused); + void humminbird_track_cb(const Waypoint* wpt); + + /* Data Members */ + + QVector humminbirdht_args = { + }; +}; + +#endif // HUMMINBIRD_H_INCLUDED_ diff --git a/vecs.h b/vecs.h index e610ce505..0a25e21a7 100644 --- a/vecs.h +++ b/vecs.h @@ -38,6 +38,7 @@ #include "ggv_bin.h" #include "globalsat_sport.h" #include "gpx.h" +#include "humminbird.h" #include "gtrnctr.h" #include "html.h" #include "kml.h" @@ -111,8 +112,6 @@ extern ff_vecs_t destinator_poi_vecs; extern ff_vecs_t destinator_itn_vecs; extern ff_vecs_t destinator_trl_vecs; extern ff_vecs_t igo8_vecs; -extern ff_vecs_t humminbird_vecs; -extern ff_vecs_t humminbird_ht_vecs; extern ff_vecs_t mapasia_tr7_vecs; extern ff_vecs_t gnav_trl_vecs; extern ff_vecs_t navitel_trk_vecs; @@ -298,8 +297,8 @@ private: LegacyFormat destinator_trl_fmt {destinator_trl_vecs}; ExifFormat exif_fmt; LegacyFormat igo8_fmt {igo8_vecs}; - LegacyFormat humminbird_fmt {humminbird_vecs}; - LegacyFormat humminbird_ht_fmt {humminbird_ht_vecs}; + HumminbirdFormat humminbird_fmt; + HumminbirdHTFormat humminbird_ht_fmt; LegacyFormat mapasia_tr7_fmt {mapasia_tr7_vecs}; LegacyFormat gnav_trl_fmt {gnav_trl_vecs}; LegacyFormat navitel_trk_fmt {navitel_trk_vecs}; -- 2.30.2